//
//  CSBigImage.m
//  classroom
//
//  Created by Macx on 16/7/31.
//  Copyright © 2016年 LiuZhuo. All rights reserved.
//

#import "CSBigImage.h"
@interface CSBigImage()<UIScrollViewDelegate>

@end
@implementation CSBigImage

//原始尺寸
static CGRect oldframe;
static CGRect _oldSframe;

/**
 *  浏览大图
 *
 *  @param scanImageView 图片所在的imageView
 */

static UIImageView * _imageView;;
static CGAffineTransform  _trs;
static UIScrollView * _sc;

+(void)scanBigImageWithImageView:(UIImageView *)currentImageview{
    //当前imageview的图片
    UIImage *image = currentImageview.image;
    //当前视图
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    //背景
    UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    [backgroundView setBackgroundColor:[UIColor colorWithRed:107/255.0 green:107/255.0 blue:99/255.0 alpha:0.6]];
    //此时视图不会显示
    [backgroundView setAlpha:0];
    //当前imageview的原始尺寸->将像素currentImageview.bounds由currentImageview.bounds所在视图转换到目标视图window中，返回在目标视图window中的像素值
    oldframe = [currentImageview convertRect:currentImageview.bounds toView:window];
    //将所展示的imageView重新绘制在Window中
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:oldframe];
    _imageView =imageView;
    imageView.multipleTouchEnabled = YES;
    [imageView setImage:image];
    [imageView setTag:0];
    [backgroundView addSubview:imageView];
    //将原始视图添加到背景视图中
    [window addSubview:backgroundView];
    
    
    //添加点击事件同样是类方法 -> 作用是再次点击回到初始大小
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideImageView:)];
    [backgroundView addGestureRecognizer:tapGestureRecognizer];
    
    //动画放大所展示的ImageView
    
    [UIView animateWithDuration:0.4 animations:^{
        CGFloat y,width,height;
        y = ([UIScreen mainScreen].bounds.size.height - image.size.height * [UIScreen mainScreen].bounds.size.width / image.size.width) * 0.5;
        //宽度为屏幕宽度
        width = [UIScreen mainScreen].bounds.size.width;
        //高度 根据图片宽高比设置
        height = image.size.height * [UIScreen mainScreen].bounds.size.width / image.size.width;
        _oldSframe =CGRectMake(0, y, width, height);
        [imageView setFrame:_oldSframe];
        UIScrollView * sc = [[UIScrollView alloc]initWithFrame:_oldSframe];
        _sc=sc;
        sc.showsHorizontalScrollIndicator = NO;
        sc.showsVerticalScrollIndicator = NO;
        _trs=sc.transform;
        imageView.frame = [backgroundView convertRect:imageView.frame toView:sc];
        [sc addSubview:imageView];
//        DDLogDebug(@"%@",imageView.superview);
        imageView.userInteractionEnabled = YES;
        [backgroundView addSubview:sc];
        //重要！ 将视图显示出来
        [backgroundView setAlpha:1];
    } completion:^(BOOL finished) {
        UIPinchGestureRecognizer *pinch=[[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinch:)];
        UIRotationGestureRecognizer *tota=[[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rato:)];
        [imageView addGestureRecognizer:pinch];
        [imageView addGestureRecognizer:tota];
        
    }];
    
}
// 缩放方法
+(void)pinch:(UIPinchGestureRecognizer *)sender
{
    float scale;
    if (sender.scale >=1) {
        scale = (sender.scale -1)/2 +1;
        if (scale>1.25) {
            scale = 1.3;
        }
    }else{
        scale = 1- (1 - sender.scale)/2 ;
    }
    _sc.transform=CGAffineTransformScale(_trs, scale,scale);//捏合的比例放大缩小   在原始图片大小的比例进行缩放
    DDLogDebug(@"%f",sender.scale);
    DDLogDebug(@"%f-------",scale);
    if (scale<1.0) {
        [UIView animateWithDuration:0.5 animations:^{
             _sc.transform = CGAffineTransformScale(_trs, 1.0, 1.0);
        }];
//    }else if (scale>1.25){
//    
//        [UIView animateWithDuration:0.5 animations:^{
//            _sc.transform = CGAffineTransformScale(_trs, 1.3, 1.3);
//        }];
    }
    _sc.contentOffset = CGPointMake(0,-_sc.frame.origin.y);
    _sc.contentSize = CGSizeMake(_sc.width,0);
    _sc.x = 0;

}
//旋转方法
+(void)rato:(UIRotationGestureRecognizer *)sender
{
    _sc.transform=CGAffineTransformRotate(_trs, sender.rotation/15);
    [UIView animateWithDuration:0.5 animations:^{
        _sc.transform =  CGAffineTransformIdentity;
        // 多次测试不加这句会出现不归零BUG
        _sc.x = 0;
    }];
}
/**
 *  恢复imageView原始尺寸
 *
 *  @param tap 点击事件
 */
+(void)hideImageView:(UITapGestureRecognizer *)tap{
    UIView *backgroundView = tap.view;
    //原始imageview
    UIImageView *imageView = [tap.view viewWithTag:0];
    //恢复
    [UIView animateWithDuration:0.4 animations:^{
        [imageView setFrame:oldframe];
        [backgroundView setAlpha:0];
    } completion:^(BOOL finished) {
        //完成后操作->将背景视图删掉
        [backgroundView removeFromSuperview];
    }];
}
@end

